﻿@using Covenant.Core
@using Covenant.Models.Listeners
@using Covenant.Models.Launchers
@model Launcher
@{
    Layout = "_LauncherLayout";
    ViewData["Title"] = "Launchers";
    IEnumerable<Listener> Listeners = ViewBag.Listeners;
}

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3">
    <h1 class="h2">@Html.DisplayFor(model => model.Name) Launcher</h1>
</div>

<ul class="nav nav-tabs mb-3" id="launcher-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="generate-tab" data-toggle="tab" href="#generate" role="tab" aria-controls="generate" aria-selected="true">
            <span data-feather="zap"></span>
            Generate
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="host-tab" data-toggle="tab" href="#host" role="tab" aria-controls="host" aria-selected="false">
            <span data-feather="cloud-lightning"></span>
            Host
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="code-tab" data-toggle="tab" href="#code" role="tab" aria-controls="code" aria-selected="false">
            <span data-feather="code"></span>
            Code
        </a>
    </li>
</ul>

<div class="tab-content" id="launcher-tab-content">
    <div class="tab-pane fade" id="generate" role="tabpanel" aria-labelledby="generate-tab">
        @{
            await Html.RenderPartialAsync("_LauncherForm", Model);
        }
        @{
            await Html.RenderPartialAsync("_LauncherCopy", Model);

            if (Model.Name == "PowerShell")
            {
                await Html.RenderPartialAsync("_EncodedLauncherCopy", Model);
            }
        }
    </div>
    <div class="tab-pane fade" id="host" role="tabpanel" aria-labelledby="host-tab">
        @{
            string content = "";
            if (Model.Name == "Binary")
            {
                content = Model.Base64ILByteString;
            }
            else if (Model.Name == "PowerShell")
            {
                string code = ((PowerShellLauncher)Model).PowerShellCode;
                content = string.IsNullOrEmpty(code) ? "" : Convert.ToBase64String(Common.CovenantEncoding.GetBytes(code));
            }
            else if (Model.Name == "InstallUtil")
            {
                string code = ((DiskLauncher)Model).DiskCode;
                content = code == null ? "" : code;
            }
            else if (new List<string> { "Cscript", "InstallUtil", "MSBuild", "Mshta", "Regsvr32", "Wmic", "Wscript" }.Contains(Model.Name))
            {
                string code = ((DiskLauncher)Model).DiskCode;
                content = string.IsNullOrEmpty(code) ? "" : Convert.ToBase64String(Common.CovenantEncoding.GetBytes(code));
            }
            ViewBag.HostContent = content == null ? "" : content;
            await Html.RenderPartialAsync("_LauncherHost", Model);
        }
        @{
            await Html.RenderPartialAsync("_LauncherCopy", Model);

            if (Model.Name == "PowerShell")
            {
                await Html.RenderPartialAsync("_EncodedLauncherCopy", Model);
            }
        }
    </div>
    <div class="tab-pane fade" id="code" role="tabpanel" aria-labelledby="code-tab">
        <div class="form-group col-md-12">
            <label for="copy-stager-code">StagerCode</label>
            <textarea id="copy-stager-code" name="copy-stager-code">@Model.StagerCode</textarea>
        </div>
        <div class="form-group col-md-12">
            <button type="button" class="btn btn-primary copy-code-button">
                <span data-feather="clipboard"></span>
                Copy
            </button>
        </div>
    </div>
</div>

@section Scripts {
    <script src="~/js/bootstrap-datetimepicker.min.js"></script>
    <script src="~/dist/clipboard.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var editor = CodeMirror.fromTextArea(document.getElementById("copy-stager-code"), {
                lineNumbers: true,
                readOnly: true,
                mode: "text/x-csharp"
            });
            
            function getCodeMirrorNative(target) {
                var _target = target;
                if (typeof _target === 'string') {
                    _target = document.querySelector(_target);
                }
                if (_target === null || !_target.tagName === undefined) {
                    throw new Error('Element does not reference a CodeMirror instance.');
                }

                if (_target.className.indexOf('CodeMirror') > -1) {
                    return _target.CodeMirror;
                }

                if (_target.tagName === 'TEXTAREA') {
                    return _target.nextSibling.CodeMirror;
                }

                return null;
            };

            var clipboard1 = new ClipboardJS('.copy-code-button', {
                text: function(trigger) {
                    return getCodeMirrorNative($("#copy-stager-code")[0]).getDoc().getValue();
                }
            });
            var clipboard2 = new ClipboardJS(".copy");

            $(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function() {
                editor.refresh();
            });

            $(".nav-link").removeClass("active");
            $("#nav-launchers").addClass("active");
            $("#generate-tab").tab('show');
            $('#KillDate').datetimepicker();
            $("#download").click(function(e) {
                e.preventDefault();
                @{
                    var url = @Model.Name + "Download";
                }
                window.location.href = '../@url';
            });
            function swapOptions() {
                var comm = $("#ImplantTemplateId").children("option:selected").text();
                if (comm.includes("HTTP"))
                {
                    $("#http-div").removeAttr("hidden");
                    $("#smb-div").attr("hidden", "true");
                }
                else if (comm.includes("SMB"))
                {
                    $("#http-div").attr("hidden", "true");
                    $("#smb-div").removeAttr("hidden");
                }
                else
                {
                    $("#http-div").attr("hidden", "true");
                    $("#smb-div").attr("hidden", "true");
                }
            };
            $("#ImplantTemplateId").change(swapOptions);
            swapOptions();
        });
    </script>
}
